<?php

namespace apiend\controllers;

use Yii;
use apiend\components\Controller;
use common\base\ModelHttpException;
use apiend\models\DeviceForm;
use common\logics\navigation\ThemeLogic;
use common\logics\navigation\TopBarLogic;
use common\logics\PositionLogic;
use common\helpers\Url;
use common\models\app\AppVersion;
use common\models\app\AppBootSplash;

/**
 * Site controller
 * 
 * @author emhome <emhome@163.com>
 * @since 1.0
 */
class SiteController extends Controller {

    /**
     * Declares the allowed HTTP verbs.
     * Please refer to [[VerbFilter::actions]] on how to declare the allowed verbs.
     * @return array the allowed HTTP verbs.
     */
    protected function verbs() {
        return [
            '*' => ['GET'],
            'register' => ['POST'],
        ];
    }

    /**
     * Displays homepage.
     * @return mixed
     */
    public function actionIndex() {
        return Yii::$app->name;
    }

    /**
     * 站点首页
     * @return mixed
     */
    public function actionHome() {
        return [
            'broadcast' => PositionLogic::current(1),
            'recommend' => PositionLogic::current(2),
        ];
    }

    /**
     * 获取当前应用版本信息
     * @return mixed
     */
    public function actionVersion() {
        return AppVersion::getLatest();
    }

    /**
     * 获取当前应用版本信息
     * @return mixed
     */
    public function actionBootsplash() {
        return AppBootSplash::getLatest();
    }

    /**
     * 注册设备信息（推送消息时使用）
     * @return mixed
     */
    public function actionRegister() {
        $model = new DeviceForm();
        if (!$model->load(Yii::$app->request->post(), '')) {
            throw new ModelHttpException('提交时数据未加载成功');
        }
        $model->device = $this->token->device;
        return $model->register();
    }

}
